//
//  TalkFirstViewController.m
//  A段项目
//
//  Created by dlios on 15/7/10.
//  Copyright (c) 2015年 dlios. All rights reserved.
//

#import "TalkFirstViewController.h"
#import "BaseHeader.h"
#import "ContentFirTableViewCell.h"
#import "SANetWorkingTask.h"
#import <SDWebImage/UIImageView+WebCache.h>
#import "TalkSecViewController.h"

@interface TalkFirstViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *array;

@end

@implementation TalkFirstViewController

- (void)dealloc
{
    [_table release];
    [_array release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        self.array = [NSMutableArray array];
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.translucent = NO;
    self.navigationItem.title = @"论坛";
    [self createTable];
    // Do any additional setup after loading the view.
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeidth - 100) style:UITableViewStylePlain];
    _table.rowHeight = 320;
    [_table registerClass:[ContentFirTableViewCell class] forCellReuseIdentifier:@"a"];
    _table.dataSource = self;
    _table.delegate = self;
    [self.view addSubview:_table];
    [_table release];
    
    [SANetWorkingTask request:@"http://api.ycapp.yiche.com/appforum/cheyouhome/?deviceid=&restype=refreshB00000612AE35EF525E5838D3B7A5C0A" parmater:nil block:^(NSDictionary *result) {
        NSArray *arr = [result objectForKey:@"data"];
        [_array addObjectsFromArray:arr];
        [_table reloadData];
    }];
    
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ContentFirTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"a"];
    NSDictionary *big = [_array objectAtIndex:indexPath.row];
    [cell.userAvatar sd_setImageWithURL:[big objectForKey:@"userAvatar"] placeholderImage:[UIImage imageNamed:@"1.jpg"]];
    cell.userName.text = [big objectForKey:@"userName"];
    cell.forumName.text = [NSString stringWithFormat:@"来自%@", [big objectForKey:@"forumName"]];
    cell.content.text = [big objectForKey:@"content"];
    cell.location.text = [big objectForKey:@"location"];
    cell.agreeCount.text = [NSString stringWithFormat:@"%@", [big objectForKey:@"agreeCount"]];
    cell.commentCount.text = [NSString stringWithFormat:@"%@", [big objectForKey:@"commentCount"]];
    cell.more.tag = indexPath.row;
    [cell.more addTarget:self action:@selector(moreAction:) forControlEvents:UIControlEventTouchUpInside];
    NSString *content = @"";
    NSArray *arr = [big objectForKey:@"comment"];
    if (arr.count > 0) {
        content = [NSString stringWithFormat:@"%@:  %@", [arr[0] objectForKey:@"userName"], [arr[0] objectForKey:@"content"]];
    }
    if (arr.count > 2) {
        content = [NSString stringWithFormat:@"%@:  %@\n%@:  %@\n%@:  %@",[arr[0] objectForKey:@"userName"], [arr[0] objectForKey:@"content"], [arr[1] objectForKey:@"userName"], [arr[1] objectForKey:@"content"], [arr[2] objectForKey:@"userName"], [arr[2] objectForKey:@"content"]];
    }
    if (content.length == 0) {
        [cell.more removeFromSuperview];
    }
    cell.comment.text = content;
    
    return cell;
    
    
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    TalkSecViewController *sec = [[TalkSecViewController alloc] init];
    NSMutableDictionary *dic = [_array objectAtIndex:indexPath.row];
    sec.dic = dic;
    [self.navigationController pushViewController:sec animated:YES];
    [sec release];
}

- (void)moreAction:(UIButton *)sender
{
    TalkSecViewController *sec = [[TalkSecViewController alloc] init];
    NSMutableDictionary *dic = [_array objectAtIndex:sender.tag];
    sec.dic = dic;
    [self.navigationController pushViewController:sec animated:YES];
    [sec release];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
